import { getAction, postAction, httpAction, deleteAction, } from '@/api/manage'

export const Mixin = {
  data() {
    return {
      /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
      queryParams: {},
      /* 数据源 */
      dataSource: [],
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
      /* table加载状态 */
      loading: false,
      /* table选中keys*/
      selectedRowKeys: [],
      /* table选中records*/
      selectionRows: [],
      systemTypeName: "全部", // 全部
    }
  },
  onLoad: function () {
    this.loadData()
  },
  onReady: function () {
  },
  methods: {
    // 加载数据
    loadData() {
      if (
        this.urls.list
      ) {
        this.loading = true;
        let params = this.getQueryParams();//页码参数
        getAction(this.urls.list, params)
          .then((res) => {
            this.loading = false;
            if (res.data && res.data.records) {
              this.dataSource = res.data.records
              this.ipagination.total = res.data.total || 0;
            }
            else {
              this.dataSource = res.rows;
              this.ipagination.total = res.total || 0;
            }
          })
          .finally(() => {
            this.loading = false;
          });
      }
    },
    // 查询条件
    getQueryParams() {
      //获取查询条件
      let param = Object.assign({}, this.queryParams);
      param.pageNo = this.ipagination.current;
      param.pageNum = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      return param
    },
    // 查询
    searchQuery() {
      let params = this.getQueryParams();//查询条件
      this.loadData(params);
    },
    // 重置
    resetQuery() {
      this.queryParams = {}
      this.loadData();
    },
    // 状态筛选
    handleStatus(val) {
      this.systemTypeName = val.label;
      this.queryParams.status = val.value;
      this.queryParams.pageNum = 1;
      this.searchQuery();
    },
    // tableIndex 计算
    getIndex(index) {
      return (this.ipagination.current - 1) * this.ipagination.pageSize + index + 1
    },
    // 多选监听
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys.map((item) => item.id);
      this.selectionRows = selectedRowKeys;
    },

    // 清空多选
    onClearSelected() {
      this.selectedRowKeys = [];
      this.selectionRows = [];
    },
    // 新增
    handleAdd() {
      this.$refs.modalForm.title = "新增";
      this.$refs.modalForm.disabledForm = false;
      this.$refs.modalForm.add();
    },
    // 编辑
    handleEdit(record) {
      if (this.urls.detail) {
        this.loading = true;
        getAction(`${this.urls.detail}${record.id}`)
          .then((res) => {
            this.loading = false;
            this.$refs.modalForm.title = "编辑";
            this.$refs.modalForm.disabledForm = false;
            this.$refs.modalForm.edit(res.data);
          })
          .finally(() => {
            this.loading = false;
          });
      }
      else {
        this.$refs.modalForm.title = "编辑";
        this.$refs.modalForm.disabledForm = false;
        this.$refs.modalForm.edit(record);
      }
    },
    // 查看
    handleRead(record) {
      if (this.urls.detail) {
        this.loading = true;
        getAction(`${this.urls.detail}${record.id}`)
          .then((res) => {
            this.loading = false;
            this.$refs.modalForm.title = "详情";
            this.$refs.modalForm.disabledForm = true;
            this.$refs.modalForm.edit(res.data);
          })
          .finally(() => {
            this.loading = false;
          });
      }
      else {
        this.$refs.modalForm.title = "详情";
        this.$refs.modalForm.disabledForm = true;
        this.$refs.modalForm.edit(record);
      }

    },
    // 删除
    handleDelete(id, url = '') {
      // 清空多选
      this.onClearSelected()
      if (url == '') url = this.urls.delete
      let that = this;
      this.$confirm('此操作将永久删除该数据, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        that.loading = true
        deleteAction(`${url}${id}`).then((res) => {
          if (res.code == 200) {
            that.loading = false
            that.$modal.msgSuccess("删除成功!");
            that.loadData();
          } else {
            that.loading = false
            that.$modal.msg(res.msg);
          }
        });
      }).finally(() => {
        that.loading = false
      })
    },

    /*****/
    // 批量操作
    batchDel() {
      if (!this.urls.deleteBatch) {
        this.$modal.msgError("请设置url.deleteBatch属性!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$modal.msg('请选择一条记录！');
        return;
      } else {
        let ids = this.selectedRowKeys.join()
        let that = this;
        this.$confirm("是否删除选中数据?", "确认删除", {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          that.loading = true;
          deleteAction(`${that.url.deleteBatch}${ids}`).then((res) => {
            if (res.code == 200) {
              //重新计算分页问题
              that.$modal.msgSuccess("删除成功!");
              that.loading = false;
              that.loadData();
              that.onClearSelected();
            } else {
              that.$modal.msg(res.msg);
              that.loading = false;
              that.loadData();
            }

          }).finally(() => {
            that.loading = false
          });
        })

      }
    },
    exportRecords() {
      this.$modal
        .confirm("是否确认导出所有数据？")
        .then(() => {
          this.loading = true;
          return getAction(`${this.urls.export}`);
        })
        .then((response) => {
          console.log(response);
          this.$download.name(response.msg);
          this.loading = false;
        })
        .catch(() => { });
    },
    recurData(list, newArr) {
      newArr.map((item) => {
        list.map((row) => {
          if (item.id.toString() === row.parentId || item.id === row.parentId) {
            if (!item.hasOwnProperty("children")) {
              item.children = [];
            }
            item.children.push(row);
          }
        });
        if (item.hasOwnProperty("children")) {
          this.recurData(list, item.children);
        }
      });
      return newArr;
    },
    // 码表回显
    returnValue(value, arr=[]) {
      let res = "-";
      arr.forEach((item) => {
        if (item.dictValue == value) res = item.dictLabel;
      });
      return res;
    },
    getStatus(value,list) {
      let res = "";
      list.forEach((item) => {
        if (item.key == value) res = item.value;
      });
      return res;
    },
  }
}
